<?php
class sitesControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$options=M('options')->children(0,"sitearea");
		if($options){
			foreach($options as $k=>$v){
				if($v['child']){
					foreach($v['child'] as $kk=>$vv){
						$vv['sites']=M('sites')->select(array(
							"where"=>"cityid=".$vv['id']." AND is_open=1 AND status=0  ",
							"order"=>"siteid DESC"
						));
						$v['child'][$kk]=$vv;
					}
				}
				$options[$k]=$v;
			}
		}
		$site=M('sites')->selectRow("siteid=".SITEID);
		$sdata=array(
			"citys"=>$options,
			"site"=>$site
		); 
		if(get_post('ajax')){
			$this->goALL("success",0,$sdata); 
		}
		$this->smarty->assign($sdata);
		$this->smarty->display("sites/index.html");
	}
	
	public function onSearch(){
		$keyword=get('keyword','h');
		if(empty($keyword)){
			$this->goAll("找不到",1);
		}
		$data=M("sites")->select(array(
			"where"=>" sitename like '%".$keyword."%' AND is_open=1",
			"limit"=>10
		));
		if($data){
			$this->goAll("success",0,$data);
		}else{
			$this->goAll("找不到",1);
		}
	}
	
	public function onNear(){
		$data=M("sites")->select(array("where"=>" is_open=1"));
		$lat=get('lat','h');
		$lng=get('lng','h');
		if($data){
			foreach($data as $k=>$v){
				if($v['lng']==0){
					unset($data[$k]);
				}else{
					$distance[$k]=$v['distance']=distanceByLnglat($lng,$lat,$v['lng'],$v['lat']);
				}
			}
			array_multisort ( $distance ,  SORT_ASC  ,  $data );
			$this->goAll("成功",0,$data[0]);
		}else{
			$this->goALl("error",1);
		}
	}
	
	public function onGet(){
		$siteid=max(1,get('siteid'));
		$data=M("sites")->selectRow("siteid=".$siteid);
		$this->goALl("success",0,$data);
	}
	
}
